CREATE PROCEDURE [acms].[User_Put]
	@Id INT, 
	@SiteId INT, 
	@UserName VARCHAR (50), 
	@FirstName VARCHAR (50), 
	@LastName VARCHAR (50), 
	@DateOfBirth DATETIME, 
	@EmailAddress VARCHAR (100), 
	@EmailAuthenticated BIT,
	@Gender TINYINT, 
	@Address1 VARCHAR (100), 
	@Address2 VARCHAR (100), 
	@Town VARCHAR (50), 
	@PostCode VARCHAR (20), 
	@County VARCHAR (20), 
	@MobileTel VARCHAR (20), 
	@Created DATETIME, 
	@Password VARCHAR (255), 
	@Salt INT, 
	@CustomBool1 BIT,
	@CustomBool2 BIT,
	@CustomBool3 BIT,
	@CustomInt1 INT,
	@CustomInt2 INT,
	@CustomInt3 INT,
	@CustomString1 VarChar(255),
	@CustomString2 VarChar(255),
	@CustomString3 VarChar(255),
	@Locked bit,
	@LastLogin DateTime,
	@LastActivity DateTime,
	@LastChangedPassword DateTime,
	@LastLocked DateTime,
	@ConcurencyId TIMESTAMP, 
	@IdOut INT OUTPUT, 
	@ConcurencyIdOut TIMESTAMP OUTPUT
AS
	if(@SiteId = 0)
		set @SiteId = null
		
	if exists (select null from User_View with (nolock) where Id = @Id) begin
		update Users
		Set
			SiteId = @SiteId,
			FirstName = @FirstName,
			LastName = @LastName,
			UserName = @UserName,
			DateOfBirth = @DateOfBirth,
			EmailAddress = @EmailAddress,
			EmailAuthenticated = @EmailAuthenticated,
			Gender = @Gender,
			Address1 = @Address1,
			Address2 = @Address2,
			Town = @Town,
			PostCode = @PostCode,
			County = @County,
			MobileTel = @MobileTel,
			Password = @Password,
			Salt = @Salt,
			CustomBool1 = @CustomBool1,
			CustomBool2 = @CustomBool2,
			CustomBool3 = @CustomBool3,
			CustomInt1 = @CustomInt1,
			CustomInt2 = @CustomInt2,
			CustomInt3 = @CustomInt3,
			CustomString1 = @CustomString1,
			CustomString2 = @CustomString2,
			CustomString3 = @CustomString3,
			Locked = @Locked,
			LastLogin = @LastLogin,
			LastActivity = @LastActivity,
			LastChangedPassword = @LastChangedPassword,
			LastLocked = @LastLocked
		Where Id = @Id and VersionId = @ConcurencyId
		if @@RowCount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else begin
		insert into Users
			(SiteId, FirstName, LastName, UserName, DateOfBirth, EmailAddress, Gender, Address1, Address2, Town, PostCode, County, MobileTel, Created, Password, Salt, CustomBool1, CustomBool2, CustomBool3, CustomInt1, CustomInt2, CustomInt3, CustomString1, CustomString2, CustomString3, Locked, LastLogin, LastActivity, LastChangedPassword, LastLocked)
		values
			(@SiteId, @FirstName, @LastName, @UserName, @DateOfBirth, @EmailAddress, @Gender, @Address1, @Address2, @Town, @PostCode, @County, @MobileTel, @Created, @Password, @Salt, @CustomBool1, @CustomBool2, @CustomBool3, @CustomInt1, @CustomInt2, @CustomInt3, @CustomString1, @CustomString2, @CustomString3, @Locked, @LastLogin, @LastActivity, @LastChangedPassword, @LastLocked)
		set @IdOut = @@Identity
	end
	
	select @ConcurencyIdOut = VersionId From User_View with (nolock) where Id = @IdOut
	
RETURN 0;